<?php
/**
 * class CommentsModel 
 */
class CommentsModel extends Model implements IFeed{

    function __construct() {
        parent::__construct();
       // $this->addObserver($this->feedobserver);
    }
    public function getAllCommentsOfPage($page) {
                
        return $this->db->query("SELECT *, DATE_FORMAT(FROM_UNIXTIME(date), '<span class=\"year\">%d.%m.%y</span> <b>%H:%I:%S</b>') as n_date FROM comments WHERE page = '".$page."'");     
    }
    public function getAllCommentsOfPagePager($page, Pager $pager) {
        $pager->setTotalNumOfItems($this->getNumOfCommentsOnPage($page));        
        return $this->db->query("SELECT * FROM comments WHERE page = '".$page."' LIMIT ".$pager->getLimitFrom().",".$pager->getLimitTo()."");     
    }
    public function getNumOfCommentsOnPage($page) {
        $total = $this->db->query("SELECT COUNT(*) AS num FROM comments WHERE page = '".$page."'");
        $total = $total->row['num'][0];
        return $total;
    }
    public function addComment($data) {
        $result = $this->db->query("INSERT INTO comments (page, `from`, message, date) VALUES ('".$data['page']."', '".$data['from']."', '".$data['message']."', '".time()."')");    
       
//        if ($result)
//            $this->fireEvent('feed', array('what' => mysql_insert_id(), 'action' => __METHOD__));
        
        return $result;
    }
    public function getCommentById($id) {
        $data = $this->db->query("SELECT * FROM comments WHERE comment_id='" . intval($id) . "' LIMIT 1");
         return $data->row;
    }
     /**
     * Реализация интерфейса IFeed
     * @param type $id
     * @return boolean 
     */
    public function getFeedInfo($actionName, $what_id, $num = 1) {
        switch ($actionName) {
            case 'addComment':
                 
                if ($num == 1) {
                    
                     $data = $this->getCommentById($what_id);
                     return array('text' => "%who добавил комментарий к странице %what", 'who_link' => 'LR/profile/show/%who', 'what_link' => $data['page']);
                } else {
                    $data = $this->getCommentById($what_id);
                     return array('text' => "%who добавил ".$num." комментариев(-я) к странице %what", 'who_link' => 'LR/profile/show/%who',  'what_link' => $data['page']);
                }

                
                
                

            default:
                return;
        }
    }
    /**
     * Реализация интерфейса IFeed
     * @param type $id
     * @return boolean 
     */
    public function getNameById($id) {
        if ($data = $this->getCommentById($id)) {
            preg_match("/^[^\s\/]+/ui", $data['page'], $results);
            require_once DIR_APPLICATION.'pages/Controller/class.'.$results[0].'Controller.php';
          
            $class = $results[0].'Controller';

            return $class::getPageName($data['page']);
        }
        else
            return false;
    }
}




?>